package com.example.pungolo.journal.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.graphics.Rect;

public class DiaryPage {
  int day;
  String imagePath;
  final int[] crop = new int[4];
  final List<DiaryEntry> entries = new ArrayList<DiaryEntry>();
  
  public void setDay(int day) {
    this.day = day;
  }

  public int getDay() {
    return day;
  }

  public String getImagePath() {
    return imagePath;
  }

  public int getEntryCount() {
    return entries.size();
  }

  public DiaryEntry getEntry(int index) {
    return entries.get(index);
  }

  public DiaryEntry getDiaryEntryByDate(Date date) {
    int c = getEntryCount();
    for (int i = 0; i < c; i++)
      if (getEntry(i).getDate().equals(date))
        return getEntry(i);
    return null;
  }

  public int[] getCropData() {
    return crop;
  }

  public Rect getCropRect() {
    return getCropRect(crop);
  }

  public static Rect getCropRect(int[] crop) {
    if (crop == null || crop[2] == 0 || crop[3] == 0)
      return null;
    else
      return new Rect(crop[0], crop[1], crop[0] + crop[2], crop[1] + crop[3]);
  }

  public void setImagePath(String image) {
    this.imagePath = image;
  }

  public void setCropData(int[] cropData) {
    if (cropData == null) {
      crop[0] = crop[1] = crop[2] = crop[3] = 0;
      return;
    }
    if (cropData.length != 4)
      throw new IllegalArgumentException();
    for (int i = 0; i < 4; i++) {
      crop[i] = cropData[i];
    }
    // TODO: salva la versione ritagliata dell'immagine da qualche parte per
    // migliorare le prestazioni.
  }

  public void addEntry(DiaryEntry entry) {
    entries.add(entry);
  }

  public void save() {
    DiaryIO.getDatabase().saveDiaryPage(this);
  }

  public boolean delete() {
    return DiaryIO.getDatabase().deleteDiaryPage(this);
  }
}
